{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*The apparent diffusion coefficient is a measure compute from diffusion-weighted images and supposed to represent real physical values. See https://radiopaedia.org/articles/diffusion-weighted-imaging-1 and https://radiopaedia.org/articles/apparent-diffusion-coefficient-1 for more information.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This script computes an ADC images from two DWI images. Let's take a look at our source images.\n",
    "<table>\n",
    "<tr>\n",
    "<td><img alt=\"DWI b0\" src=\"images/b0.png\" width=\"200\" /></td>\n",
    "<td><img alt=\"DWI b1000\" src=\"images/b1000.png\" width=\"200\" /></td>\n",
    "</tr>\n",
    "<tr><td>DWI b0</td><td>DWI b1000</td></tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The scripts requires two input files and the b-value of the second image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "usage: medpy_apparent_diffusion_coefficient.py [-h] [-t THRESHOLD] [-v] [-d]\n",
      "                                               [-f]\n",
      "                                               b0image bximage b output\n",
      "\n",
      "Computes the apparent diffusion coefficient from two diffusion weighted\n",
      "MRI images. The output image will be of type float.\n",
      "\n",
      "Normally diffusion weight (DW) MRI images are acquired once with a\n",
      "b-value of 0 (which we call b0) and once with another b-value (which we\n",
      "call bx) such as 500, 800 or 1000. The latter is typical for brain MRIs.\n",
      "This results in a single b0 DW image and three bx DW images, one for each\n",
      "direction.\n",
      "\n",
      "Usually the three bx DW images are already combined into an isotropic\n",
      "average image (which we call abx) denoting the length of the three-dimensional\n",
      "vector formed by the the three bx images.\n",
      "\n",
      "The formula presented in [1] is applied to the b0 and abx images to\n",
      "compute the apparent diffusion coefficient (ADC):\n",
      "\n",
      "ADC = -bx-value * ln(abx-image / b0-image)\n",
      "\n",
      "To cope with zero-values in the images, we apply a-priori a\n",
      "thresholding to the b0 + abx DW image, set all lower values to 0 and\n",
      "apply the formula only to the remaining intensities. Note that the\n",
      "default threshold is chosen using Otsu's and is good for most cases.\n",
      "(Thanks to Nils at the UKE in Hamburg, Germany for this hint!)\n",
      "\n",
      "We restrain from implementing a method working on more DW images, that\n",
      "were acquired with multiple b-values, as [2] observed that this might\n",
      "lead to worse results.\n",
      "\n",
      "[1] \"Understanding Diffusion MR Imaging Techniques: From Scalar\n",
      "Diffusion-weighted Imaging to Diffusion Tensor Imaging and Beyond\" by\n",
      "Patric Hagmann et al.\n",
      "[2] \"Understanding the Mathematics Involved in Calculating Apparent\n",
      "Diffusion Coefficient Maps\" by Michael Yong Park and Jae Young Byun\n",
      "\n",
      "Copyright (C) 2013 Oskar Maier\n",
      "This program comes with ABSOLUTELY NO WARRANTY; This is free software,\n",
      "and you are welcome to redistribute it under certain conditions; see\n",
      "the LICENSE file or <http://www.gnu.org/licenses/> for details.   \n",
      "                  \n",
      "\n",
      "positional arguments:\n",
      "  b0image               the diffusion weighted image required with b=0\n",
      "  bximage               the diffusion weighted image required with b=x\n",
      "  b                     the b-value used to acquire the bx-image (i.e. x)\n",
      "  output                the computed apparent diffusion coefficient image\n",
      "\n",
      "optional arguments:\n",
      "  -h, --help            show this help message and exit\n",
      "  -t THRESHOLD, --threshold THRESHOLD\n",
      "                        set a fixed threshold for the input images to mask the\n",
      "                        computation\n",
      "  -v, --verbose         verbose output\n",
      "  -d                    Display debug information.\n",
      "  -f, --force           overwrite existing files\n"
     ]
    }
   ],
   "source": [
    "medpy_apparent_diffusion_coefficient.py -h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That means we have to call it like this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "medpy_apparent_diffusion_coefficient.py resources/b0.nii.gz resources/b1000.nii.gz 1000 output/adc.nii.gz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Which results in\n",
    "<img alt=\"ADC image\" src=\"images/adc.png\" width=\"200\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The results are more impressive with actual 3D volumes, from which reliable value can be computed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Bash",
   "language": "bash",
   "name": "bash"
  },
  "language_info": {
   "codemirror_mode": "shell",
   "file_extension": ".sh",
   "mimetype": "text/x-sh",
   "name": "bash"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
